define(['init','ckeditor-jquery','fullcalendar_zh-cn','moment','bsMsgView',
    	'userSelect','codeDef','formServ','bsMsgServ','dateUtil'], 
    	function(init,ckeditor,calendarLang,moment,bsMsgView,
    		userSelect,codeDef,formServ,bsMsgServ,dateUtil) {		
			
		'use strict';	
			
		var rootid = 'message-response';
		
		//定义页面url
		var msgUrl = '/document/query/getPage.action?code=msgView';
		
		var responseUrl = '/document/query/getPage.action?code=responseView';
		
		//ckeditor对象
		var editor ;
		
		//页面传递的参数
		var openParam;
		
		var btnEvent = {
			//用户选择
			userSelect:function(e){
				userSelect.openPage();
			}
			,
			//发布
			save:function(e){
				
				if(window.console){
					console.log('save fired...')
				}
				var _f = $('#'+rootid + ' .main-form')
				if(_f.length>0){
					var _data = formServ.getDataObj({form:_f});
					
					//判断form必填项
					if(
						!formServ.validateForm({
							form:_f,
							rules:private_method.getFormRule()
						})
					){
						bsMsgServ.errorSlide('请填写表单内容');
						//释放按钮禁用状态
						$(e.currentTarget).removeClass('disabled');
						return
					}
					
					
					var usr = init.getCurrentUser();
					
					if(usr){
						if(!_data.public_user_id){
							_data.public_user_id = usr.id;						
							_data.public_user_name = usr.name;
						}
						
						if(!_data.public_dept_id){
							_data.public_dept_id = usr.deptid;
							_data.public_dept_name = usr.deptName;
						}
						
						if(!_data.typeId){
							_data.typeId = 16
							_data.typeName = '通知公告'
						}
						
						if(!_data.public_date){
							_data.public_date = dateUtil.getDateValue({format:'yyyy-MM-dd hh:mi:ss'});
						}
					}
					
					if(editor){
						_data.lob_contents = editor.getData();
					}
					
					var _d = $.toJSON(_data)
					var _param = {
						url:'/'+init.getSite()+'/docread/save.action',
						data:_d,
						type:'post',
						dataType:'json',
						contentType:'appliction/json;charset=utf-8',
						success:function(data){
							if(data){
								
								
								_f.form('load',data);
								
								if(data.lob_contents){
									if(editor){
										editor.setData(data.lob_contents);
									}
								}
								bsMsgServ.infoSlide(
									codeDef.getConfig({
										type:'operate',
										code:'success'
									})
								)
							}
							
							//释放按钮禁用状态
							$(e.currentTarget).removeClass('disabled');
						}
						,
						failure:function(){
							bsMsgServ.errorSlide(
								codeDef.getConfig({
									type:'operate',
									code:'error'
								})
							)
							//释放按钮禁用状态
							$(e.currentTarget).removeClass('disabled');
						}
					}
					$.ajax(_param);
				}
				
			}
			
			,
			cancel:function(){
				public_method.destory();				
				init.loadContainerUrl('/getPage.action?code=pageMain');
			}
			,
			//附件
			archive:function(e){
				
			}
			,
			bind_btn_click:function(e){
				init.applyBtnEvt({btnEvt:e,btnConfig:btnEvent});
			}
			,
			response:function(_options){
				
			}
			
			
			
		}
		
		var private_method = {
			
			/**
			 * 打开js预览页面
			 * @param _options.data
			 */
			openPage:function(_options){
				private_method.loadContainerUrl(responseUrl);			
			}
			,
			/**
			 * 设置表单验证规则
			 */
			getFormRule:function(){
				var rules = {
					rules:{
						name: 'required'
					}
					,
					messages:{
						name: '文档标题不允许为空'
					}
				}
				
				return rules;
			}
			
			,
			/**
			 * 加载容器页面
			 */
			loadContainerUrl:function(url){
				
//				public_method.destroy();
				init.loadContainerUrl(url);
			}	
		}
		
		
		var public_method= {
				
			/**
			 * 打开js预览页面
			 * @param _options.container	容器对象
			 * @param _options.id			业务对象id
			 */
			openPage:function(_options){
				
				openParam = _options;
				private_method.openPage();
			}
			,
			//初始化页面
			initPage:function(){
				if(window.console){
					console.log('init message page...');
				}
				
				//判断是否有传入参数
				if(openParam){
					if(openParam.name){	
						var _titleEl = $('#' + rootid + ' .page-title');
						_titleEl.html(openParam.name);
						$('#' + rootid + ' .main-form #name').val('回复: '+openParam.name);
					}
					
					
					var _pageInfo = '';						
					
					if(openParam.public_dept_name){
						_pageInfo += ' '+openParam.public_dept_name					
					}
					
					if(openParam.public_user_name){
						_pageInfo += ' ' +openParam.public_user_name
					}							
							
					if(openParam.public_date){										
						
						_pageInfo += ' ' +openParam.public_date
					}													
					
					
					if(_pageInfo){
						//文档属性
						var contentHeaderEl = $('#'+rootid + ' .page-info')
						
						if(contentHeaderEl.length==1){
							$(contentHeaderEl).html(_pageInfo)
						}
					}
				}
				
				//查询回复记录
				
				editor = init.setLobContent();
				
				var _btn = $('#'+ rootid +' .btn');
				
				if(_btn.length>0){
					_btn.unbind('click');
					_btn.bind('click',btnEvent.bind_btn_click);
				}
			},
			//销毁函数
			destory:function(){
				openParam = null;
				editor = null;
			}
		}
		
		return public_method;
    });
    
    

